WORKING EFFECTIVELY WITH LEGACY CODE

キーフレーズ

the and that you class method are can code for have public The make () test this but with the c methods new them write about tests When get need changes change they use create int interface 100k return private classes void some when there one COde will what like another Figure String more place just out ing the test object very refactoring because from instance little add start dependencies work all doesn't name system with t want design could point people testing Often outputStream has any Here your way might Method dependency example don understand really like this things different behavior This used

目次

VII CONTENTS Contents Foreword by Robert C. Martin. Preface lntroduction . PART I: The Mechanics of Change.. Chapter 1 : Changing S0ftware . Four Reasons to Change Software Risky Change . Chapter 2 : Working with Feedback What ls Unit Testing? Higher-LeveI Testing . Test Cover1ngs The Legacy Code Change AIgorithm Chapter 3 : Sensing and Separation . Faking Collaborators Chapter 4 : The Seam Model A Huge Sheet of Text Seams Seam Types . Chapter 5 : Tools. Automated Refactoring T001s Mock Objects . Unit-Testing Harnesses . General Test Harnesses . XV XV XXI 1 っ 0 4 「 / っ ~ 4 4 8 1 っ 0 9- 9- 0 っ 0 - ) 「 / 8 っ 0

VIII CONTENTS PART Ⅱ : Changing Software Chapter 6 : I Don't Have Much Time and I Have to Change lt. Sprout Method . Sprout Class . Wrap Method Wrap CIass . Summary. Chapter 7 : lt Takes Forever to Make a Change . Understanding Lag Time Breaking Dependencies Summary Chapter 8 : How DO I Add a Feature? Test-Driven DeveIopment (TDD) . Programming by Difference . S ummary Chapter 9 : I Can't Get This Class into a Test Harness . The Case of the lrritating Parameter The Case of the Hidden Dependency . The Case of the Construction Blob The Case of the lrritating GIobal Dependency . The Case of the HorribIe lnclude Dependencies . The Case of the Onion Parameter The Case of the Aliased Parameter . Chapter 10 : I Can't Run This Meth0d in a Test Harness . The Case of the Hidden Method The Case of the "HeIpful" Language Feature . The Case of the Undetectable Side Effect Chapter 11 : I Need to Make a Change. What Methods Should I Test? Reasoning About Effects Reasoning Forward Effect Propagation ・ T001S for Effect Reasoning ・ Learning from Effect Analysis Simplifying Effect Sketches . て 7 3 3 ろ . .55 . 57 . .59 . .63 . .67 . .71 . 76 . 77 . .77 . .78 .. 85 .. 8 8 . .94 . .104 . 105 . .106 .. 113 . . 116 . . 118 . .127 . .130 . .133 . 137 . .138 . .141 . .144 . 151 . .151 . .157 . .163 . .165 . .167 . .168

CONTENTS 3 Chapter 12 : I Need t0 Make Many Changes in One Area. lnterception POintS Judging Design with Pinch P0ints . Pinch Point Traps Chapter 13 : I Need to Make a Change, but I Don't Know What Tests to Write . Characterization Tests Characterizing Classes Targeted Testing A Heuristic forWriting Character1zation Tests . Chapter 14 : Dependencies on Libraries Are Killing Me Chapter 15 : My AppIication ls AII API Calls Chapter 16 : I Don't Understand the C0de Well Enough t0 Change lt Notes/Sketching Listing Markup . Scratch Refactoring Delete Unused Code Chapter 17 : My Application Has NO Structure . TeIIing the Story of the System . Naked CRC Conversation Scrut1ny Chapter 18 : My Test C0de ls in the Way . Class Naming Convent10ns . Test Location Chapter 19 : My Project ls N0t Object Oriented. How Do I Make Safe Changes?. . 231 An Easy Case . 232 A Hard Case . . 232 Adding New Behavior . 236 Taking Advantage of Object Orientation . 239 lt's AII Object Oriented . . 242 Chapter 20 : This Class ls T00 Big and I Don't Want lt t0 Get Any Bigger . 245 Seeing Responsibilities . 249 . 173 . 174 . 182 . 184 . 185 . 186 . 189 . 190 . 195 . 197 . 199 . 209 . 210 . 211 . 212 . 213 . 215 . 216 . 220 . 224 . 227 . 227 . 228 , 十 4 ー 9 く 0

。汀い 5 を CONTENTS Other Techniques . Moving Forward After Extract Class . Chapter 21 : l'm Changing the Same C0de All Over the Place First Steps Chapter 22 : I Need to Change a Monster Method and I Can't Write Tests for lt Varieties Of Monsters TackIing Monsters with Automated Refactoring Support The ManuaI Refactoring Challenge . Strategy Chapter 23 : How DO I Know That l'm N0t Break1ng Anything?. Hyperaware Editing . Single-GoaI Editing Preserve Signatures . Lean on the Compiler Chapter 24 : We Feel OverwheImed. lt lsn't G0ing t0 Get Any Better. PART III: Dependency-Breaking Techniques Chapter 25 : Dependency-Breaking Techniques . Adapt Parameter Break Out Method Object Definition Completion . Encapsulate G10bal References . Expose Static Meth0d Extract and Override Call Extract and Override Factory Meth0d Extract and Override Getter . Extract lmplementer Extract lnterface lntroduce lnstance Delegator . lntroduce Static Setter Link Substitution . Parameterize Constructor . Parameterize Method . .265 . .265 . .268 . 269 .. 272 . 289 . .290 .. 294 .. 297 . .304 . 309 . .310 . . 311 . .312 . .315 . . 319 . .323 . 325 . .326 . .330 . .337 . .339 . .345 . .348 . .350 . .352 . .356 . .362 . .369 . .372 . .377 . .379 . .383

Primitivize Parameter PuII Up Feature . Push Down Dependency . Replace Function with Function Pointer . Replace Global Reference with Getter Subclass and Override Method . Supersede lnstance Variable Template Redefinition . Text Redefinition Appendix: Refactoring ・ Extract Method Glossary . lndex CONTENTS . 385 . 388 . 392 . 396 . 399 . 401 . 404 . 408 . 412 . 415 . 415 . 421 . 423

lndex #include directives, 129 abbreviations, 284 access protection, subverting, Account, 120 , 364 ActionEvent class, 145 ACTIOReportFor, 108 141 Adapt Parameter, 142 , 326-329 adapting parameters, 326-329 addElement, 160 AddEmployeeCmd, 279 getBody, 280 write method, 274 adding features. See features, adding AGGController, 339-341 algorithms for changing legacy code, breaking dependencies, 19 finding test points, 19 identifying change points, 18 refactoring, 20 writing tests, 19 18 aliased parameters, getting classes 1ntO test harnesses, 133-136 analyzing effects, 167-168 API calls. 立ビ 4 な 0 libraries restructuring, 199-201 , 203-207 skinning and wrapping, 205-207 application architecture, preservmg, 215-216 conversation concepts, 224 Naked CRC, 220-223 telling story Of system, 216 ー 220 architecture Of system, preserving, 215-216 conversation concepts, 224 Naked CRC, 220-223 telling story of system, 216-220 automated refactoring monster methods, 294-296 tests, 46-47 automated tests, 185-186 characterization tests, 186-189 for classes, 189-190 heuristic for writing, 195 targeted testing, 190-194 Beck, Kent, 48 , 220 behavior, 5 preserving, 7 behavior Of COde. See charactenzatl()n tests 18 8 BindName method, 337 BondRegistry, 367 Brant, John, 45 Break Out Method Object, 137 , 330-336 monster methods, 304 breaking dependencies, 19-25 , 79-85 , 135 lnterception Points, 174-182 breaking up classes, 183 423

424 INDEX bug finding versus characterlzation tests, when to fix bugs, 190 bugs, fixing in software, 4-6 build dependencies, breaking, 80-85 buildMartSheet, 42 bulleted methods, 290 18 8 C macro preprocessor, testrng procedural code, 234-236 C + + , 127 compilers, 127 effect reasoning tools, 166 Template Redefinition, 410 calls, 348-349 CCAImage, 139-140 cell.RecalcuIate, 40 change points, identifying, 18 changing software. See software, changing characterization tests, 151 , 157 , 186-189 for classes, 189-190 heuristic for writing, 195 targeted testing, 190-194 characters, writing null characters, 272 classes Account, 364 ActionEvent, 145 AddEmployeeCmd, 279 AGGController, 339 big classes, 247 extracting classes from, 268 problems with, 245 refactoring, 246 responsibilities. See responsibilities breaking up, 183 CCAImage, 139-140 characterization tests, 189-190 ClassReader, 155 Command, 281-282 Coordinate, 165-166 CppClass, 156 ExternalRouter, 373 extracting, 268 tO current class first monster methods, 306 fakeConnection, 110 getting intO test harnesses aliased parameters, 133-136 global dependency, 118-126 hidden dependency, 113-116 huge parameter lists, 116-118 include dependencies, 127-130 parameters, 106-113 , 130-132 IndustrialFacility, 135 instances, 122 interfaces, extracting, 80 LoginCommand. See LoginCommand ModelNode, 357 naming conventions, 227-228 once dilemma, 198 OriginationPermit, 134-135 Packet, 345 PaydayTransaction, 362 ProductionModelNode, 358 RuIeParser, 250 Scheduler, 128 SymbolSource, 150 test harnesses, parameters, 113 testing subclasses, 227 , 390 ClassReader, 155 COde editing. See editing code effect propagation, 164-165 modularity, 29 preparing for changes, 157-163 test COde versus production code, 110 COde reuse avoiding library dependencies, 197-198 restructuring API calls, 199-207 collaborating fakes, mock objects, 27-28 Command class, 281-282 write method, 277 writeBody method, 285 Command/Query Separation, 147-149 commandChar variable, 276-277 CommoditySelectionPaneI, 296 compilers C + + , 127 editing code, 315-316 compiling Scheduler, 129

425 completing definitions, 337-338 Composed Meth0d (testing changes), 69 concrete class dependencies versus interface dependencies, 84 const keyword, 164 constructors, Parameterize Constructor, 379-382 conventions, class nammg conventlons, 227-228 Coordinate class, 165-166 coordinates, 165 coupling count, 301 ー 302 Cover and Modify, 9 Coverage, 13 CppClass, 156 CppUnitLite, 50 ー 52 CRC (CIass, Responsibility, and Collaborations), Naked CRC, 220-223 CreditMaster, 107 ー 108 CreditValidator, 107 Cunningham, Ward, 220 cursors, 116 data type converslon errors, 193-194 db—update, 36 debugging ・立 bug finding decisions, looking for, 251 declarations, 154 decorator pattern, 72 ー 73 Definition Completion, 337-338 definitions, completing, 337-338 dejection, overcoming, 319-321 delegating instance methods, 369-376 deleting unused code, 213 dependencies, 16 , 18 , 21 avoiding, 197-198 breaking. See breaking; dependency- breaking techniques gettlng classes intO test harnesses, 113-116 gleaning from monster methods, 303 global dependencies, getting classes into test harnesses, 118-126 INDEX include dependencies, getting classes into test harnesses, 127-130 in procedural COde, avoiding, 236-239 Push Down Dependency, 392-395 restructuring API calls, 199-207 dependency-breaking techniques Adapt Parameter, 326-329 Break Out Method Object, 330-336 Definition Completion, 337-338 Encapsulate Global References, 339- 344 Expose Static Method, 345-347 Extract and Override CaIl, 348-349 Extract and Override Factory Method, 350-351 Extract and Override Getter, 352-355 Extract lmplementer, 356-361 Extract lnterface, 362-368 lntroduce lnstance Delegator, 369-371 lntroduce Static Setter, 372-376 Link Substitution, 377-378 Parameterize Constructor, 379-382 Parameterize Methods, 3 83-384 Primitivize Parameter, 3 85-387 PuII Up Feature, 388-391 Push Down Dependency, 392-395 Replace Function with Function Pointer, 396-398 Replace Glob 引 Reference with Getter, 399-400 Subclass and Override Method, 401-403 Supersede lnstance Variable, 404-407 Template Redefinition, 408-411 Text Redefinition, 412-413 design, lmprovmg software design. See refactoring directories, locatlons for test COde, 228-229 draw( ) , Renderer, 332 duplication, 269-271 removing, 93-94 , 272-287 renaming classes, 284

426 INDEX Edit and Pray, 9 Edit and Pray programming, 246 editing code compilers, 315-316 hyperaware editing, 310 Pair Programming, 316 preserving signatures, 312-314 single-goal editing, 311-312 effect analysis IDE support for, 152 learning from, 167-168 effect propagation, 163-165 preventing, 165 effect reasoning, 152-157 tools for, 165-167 effect sketches, 155 , 254 pinch points, 108-184 effect sketches, simplifying, 168-171 effects, encapsulation, 171 effects of change, understanding, 212 Elements, 158 elements addElement, 160 generatelndex, 159 enabling points, 36 Encapsulate G10bal References, 239 , 315- 316 , 339-344 encapsulating global references, 339-344 encapsulation, effects, 171 encapsulation boundaries, pinch POintS as, 182-183 error localization, 12 errors changing software, 14-18 type conversion, 193-194 evaluate method, 248 exceptions, throwing, 89 execution tlme, 12 Expose Static Method, 137 , 330 , 345-347 exposing static methods, 345-347 ExternalRouter, 373 Extract and Override Call, 348-349 Extract and Override Factory Meth0d, 116 , 350-351 Extract and Override Getter, 352 , 354-355 Extract lmplementer, 71 , 74 , 80-82 , 85 , 117 , 131 , 356-361 Extract lnterface, 17 , 71 , 74 , 80 , 85 , 112-114 , 117 , 131 , 135 , 326 , 333 , 362-368 Extract Method (refactoring), 415-419 extractrng calls, 348-349 classes, 268 tO current class first, monster methods 306 factory method, 350-351 getters, 352-355 implementers, 356-361 interfaces, 362-368 monster methods, 301-302 small pieces, monster methods, 306 extracting interfaces, 80 extracting methods, 212 refactoring tools, 195 Responsibility-Based Extraction, 206- 207 targeted testing, 190-194 extractlons, redoing ln monster methods, 307 factory method, 350-351 failing test cases, writing, 8 8-91 fake objects, 23-27 distilling fakes, 27 tests, 26 FakeConnection class, 110 fakes collaborating mock objects, 27-28 distilling, 27 fake objects. 立ビ fake objects feature sketches, 252-254 features, adding, 87 with programming by difference, 94-104 with test-driven development (TDD), 88-94

427 FeeCalculator, 259 feedback, 11 testing. S ビ testlng feedback lag time, effect on length of time for changes, 78-79 6 に inclusion, testing procedural COde, 234 ー 236 finding sequences, monster methods, 305-306 test points, 19 FIT (Framework for lntegration), 53 fit. Fixture, 3 7 fit. Parse, 37 Fitnesse, 53 fixing bugs in software, 3-4 formConnection method, 404 formStyles method, 349 Fowler, Martin, 325 Framework for lntegration Tests (FIT), 53 Frameworks, 118 global dependency, 118-126 function pointers replacing, 396 ー 398 testing procedural code, 238-239 functional changes, 310 functlons PostReceiveError, 31 replacing with function pointers, 396- 398 run(), 132 send message, 114 SequenceHasGapFor, 386 substituting, 377-378 Gamma, Erich, 48 GDIBrush, 333-334 GenerateIndex, 158-162 elements, 159 generating indexes, 15 8 getBalance, 120 INDEX getBalancePoint( ) , 152 getBody, AddEmployeeCmd, 280 getDeadTime, 3 89 getDeclarationCount(), 153 getElement, 160 , 163 getElementCount, 160 , 163 getlnstance method, 120 getlnterface, 154 getKSRStreams, 142 getLastLine( ) , 27 getName, 153 ge tters extracting, 352-355 lazy getters, 354 overriding, 352-355 replacing global references, 399-400 getValidationPercent, 106 , 110 Gleaning Dependencies, monster methods 303 global dependency, getting classes into test harnesses, 118-126 global references encapsulating, 339-344 replacing with getters, 399-400 graphics libraries, link seams, 39 grouping methods, 249 hidden methods, 250 getting methods intO test harnesses, 138-141 hierarchies, permits, 134 higher-level testing, 14 , 173-174 lnterception Points, 174-182 HttpFiIeCollection, 141 HttpPostedFile objects, 141 HttpServletRequest, 327 hyperaware editing, 310 IDE, support for effect analysis, 152 identifying change points, 18 implementers, extracting, 356-361

428 INDEX include dependencies, getting classes IntO test harnesses, 127-130 independence, removing duplication, 285 indexes, generating, 158 IndustrialFacility, 135 inheritance, programming by difference, 94 ー 104 InMemoryDirectory, 158 , 161 lnstances classes, 122 lntroduce lnstance Delegator, 369-376 Supersede lnstance Variable, 404-407 testing, 123 PermitRepository, 121 lnterception P0ints, 174-182 lnterface Segregation Principle (ISP), 263 interfaces, 132 dependencies versus concrete class dependencies, 84 extracting, 80 , 362-368 naming, 364 ParameterSource, 327 segregating, 264 internal relationships,looking for, 251 lntroduce lnstance Delegator, 369-371 lntroduce Sensing Variable, 298-301 lntroduce Static Setter, 122 , 126 , 341 , 372-376 ISP (lnterface Segregation Principle), 263 Jeffries, Ron, 221 JUnit, 49-50 , 217 keywords const, 164 mutable, 167 knobs, 287 lag time, effect on length 0f time for changes, 78-79 language features, getting methods intO test harnesses, 141-144 lazy getters, 354 Lean on the Compiler, 125 , 143 , 315 legacy code, changing algorithms, 18 breaking dependencies, 19 finding test points, 19 identifying change points, 18 refactoring, 20 writing tests, 19 legacy systems versus well-maintained systems, understanding 0f code, 77 length of time for changes, 77 breaking dependencies, 79-85 reasons for, 77 ー 79 test harness usage, 57-59 Sprout Class, 63-67 Sprout Method, 59-63 Wrap CIass, 71-76 Wrap Method, 67-70 libraries. See 4 な 0 API calls dependencies, avoiding, 197-198 graphics libraries, link seams, 39 mock object libraries, 47 Link Seam, testing procedural code, 233 ー 234 link seams, 36-40 Link Substitution, 342 , 377-378 Liskov substitution principle (LSP) violation, 101 listing markup for understanding code, 211-212 LoginCommand, 278 write method, 272-273 LSP (Liskov substitution principle) violation, 101 macro preprocessor, testlng procedural code, 234-236 mail service, 113-114 manual refactoring, monster methods, 297 Break Out Method Object, 304 extracting, 301-302

429 Gleaning Dependencies, 303 lntroduce Sensing Variable, 298-301 marking up listings for understanding code, 211-212 MessageForwarder, 401 method objects, breaking out, 330-336 from monster methods, 304 method use rule, 189 methods ACTIOReportFor, 108 BindName, 337 draw( ) , Renderer, 332 effects of change, understanding, 212 evaluate, 248 Extract Method (refactoring), 415-419 extracting, 212 formConnection method, 404 formStyles, 349 getBalancePoint( ) , 152 getBody, AddEmployeeCmd, 280 getDeclarationCount(), 153 getElement, 160 , 163 getElementCount, 160 , 163 getlnstance, 120 getlnterface, 154 getKSRStreams, 142 getting intO test harnesses hidden methods, 138-141 language features, 141-144 side effects, 144-150 getValidationPercent, 110 grouping methods, 249 hidden methods, 138-141 , 250 lazy getters, 354 monster methods. S ビビ monster methods non-virtual methods, 367 Parameterize Method, 383-384 performCommand, 147-149 populate, 326 private methods, testing for, 13 8 public methods, 138 readToken, 157 recalculate, 306 recordError, 366 INDEX resetForTesting( ) , 122 Responsibility-Based Extraction, 206-207 restricted override dilemma, 198 RFDIReportFor, 108 scan(), 23-25 setUp, 50 showLine, 25 snap(), 139 Sprout Method, 246 static methods, exposlng, 345-347 Subclass and Override Method, 401-403 suspend frame, 339 targeted testing, 190 ー 194 tearDown, 375 testEmpty, 49 understanding structure of, 211 update, 296 updateBalance, 370 validate, 136 , 345 write, 273-275 AddEmployeeCmd, 274 Command class, 277 LoginCommand, 272-273 writeBody, 281 Command class, 285 writing tests for, 137 migrating t0 object orientation, 239-244 Mike Hill, 51 mock objects, 27-28 , 47 ModelNode class, 357 modularity, 29 monster methods, 289 automated refactoring, 294-296 bulleted methods, 290 extracting small pieces, 306 extracting tO current class first, 306 finding sequences, 305-306 manual refactoring. See manual refactoring redoing extractions, 307 skeletonize methods, 304-305 snarled methods, 292 ー 294 morale, increasing, 319-321

430 INDEX mutable, 167 Naked CRC, 220 ー 223 naming, 356 interfaces, 364 nammg conventlons abbreviations, 284 classes, 227 ー 228 new constructors, 3 81 non-virtual methods, 367 normalized hierarchy, 103 null characters, 272 NuIl Object Pattern, 112 NuIlEmployee, 112 nulls, 111 ー 112 NUnit, 52 0 object orientation, migrating t0, 239-244 object seams, 33 , 40-44 , 239 , 369 objects creating, 130 fake objects, 23-27 distilling, 27 tests, 26 HttpFiIeCollection, 141 HttpPostedFiIe, 141 mail service, 113-114 mock objects, 27-28 , 47 once dilemma, 198 OO languages, C + + , 127 Opdyke, Bill, 45 open/closed principle, 287 optlmizatlon, changing software, OriginationPermit, 134-135 Orthogonality, 285 overriding calls, 348-349 factory method, 350-351 getters, 352-355 6 overwhelming feelings, overcommg, 319-321 Packet class, 345 PageLayout, 348 Pair Programming, 316 paper view, 402 parameter lists, gettmg classes 1ntO test harnesses, 116-118 Parameterize Constructor, 114-116 , 126 171 , 242 , 341 , 379-382 Parameterize Method, 341 , 3 83-384 parameters adapting, 326-329 aliased parameters, 133-136 gettlng classes IntO test harnesses, 106-113 , 130-132 Parameterize Constructor, 379-382 Parameterize Method, 3 83-384 Primitivize Parameter, 385-387 ParameterSource, 327 Pass Null, 62 , 111-112 , 131 passing nulls 112 principle, 287 principles, open/closed Primitivize Parameter, 17 , 385-387 primary responsibilities, looking for, 260 preventing effect propagation, 165 signatures, 312-314 behavior, 7 preservmg 331 Preserve Signatures, 70 , 240 , 312-314 , preprocessing seams, 33-36 , 130 preparing for changes to code, 157-163 PostReceiveError, 31 , 44 populate method, 326 pointers. See function pointers testing with, 180-184 182-183 as encapsulation boundaries, pinch points, 80 PermitRepository, 120-125 Permit, hierarchies, 134 performCommand, 147-149 PaydayTransaction class, 362 Singleton Design Pattern, 372 Null Object Pattern, 112 patterns

431 private methods, testing for, 13 8 problems with big classes, 245 procedural code, testing, 231 ー 232 with C macro preprocessor, 234-36 with file inclusion, 234-236 function pointers, 23 8 -239 with Link Seam, 233-234 migrating tO Object orientation, 239-244 Test-Driven Development (TDD), 236 ー 238 production code versus test code, 110 ProductionModelNode, 35 8 programming, rediscovering fun in, 319-321 programming by difference, 94-104 propagating effects. See effect propagation methods, 297-301 manual refactoring, monster Extract Method, 415-419 big classes, 246 and tests, 46-47 monster methods, 294-296 automated refactoring refactoring, 5 , 20 , 45 , 415 methods, 307 redoing extractions, monster text, 412-413 templates, 408-411 redefining recordError, 366 recalculate method, 306 Recalculate, 40-42 reasoning forward, 157-163 reasoning forward, 157-163 t001S for, 165-167 effect reasoning, 152-157 reasomng readToken method, 157 Push Down Dependency, 392-395 PuII Up Feature, 388-391 public methods, 138 INDEX scratch refactoring, 264 refactoring tools, 45-46 , 195 scratch refactoring for understanding code, 212-213 R ビけ 0 〃 g : んゆ ro ⅵ〃 g the D おな〃 0 ー E ズな行〃 g Code (Fowler), 415 references, Encapsulate GlObal References, 339-344 regression testing, 10-11 relationships, looking for internal relationships, 251 removing duplication, 93-94 , 272-287 renaming classes, 284 renderer, draw(), 332 Replace Function with Function Pointer, 396-398 RFDIReportFor, 108 return values, effect propagation 163 restricted override dilemma, 198 206-207 Responsibility-Based Extraction, tactics for dealing with, 266 ー 268 strategy for dealing with, 265 separating, 211 segregating interfaces, 264 scratch factoring, 264 primary responsibilities, 260 responsibility, 260 looking for primary 263 ISP (lnterface Segregation PrincipIe), internal relationships, 251-253 hidden methods, 250 grouping methods, 249 can change, 251 decisions, looking for decisions that responsibilities, 249 resetForTesting( ) , 122 Reservation, 256-257 399-400 global references with getters, 396-398 functlons with function pointers, replacing 399-400 Replace Global Reference with Getter,

432 INDEX RGHConnections, 107-109 risks of changing software, 7-8 object seams, 33 , 40-44 link seams, 36-40 enabling points, 36 seams, 30-33 for understanding code scratch refactoring, 264 SchedulingTask, 131-132 SchedulerDisplay, 130 compiling, 129 Scheduler, 128-129 , 391 scan(), 23-25 safety nets, 9 run(), 132 RuleParser class, 250 Roberts, Don, 45 , 212-213 side effects, getting methods into test showLine, 25 setUp method, 50 setTestingInstance, 121-123 setSnapRegion, 140 305-306 sequences, finding in monster methods SequenceHasGapFor, 386 separation, 21-22 separating responsibilities, 211 sensing variables, 301 , 304 sensing, 21-22 send message function, 114 segregating interfaces, 264 preprocessing seams, 33-36 skeletonize methods, 304-305 120 , 372 Singleton Design Pattern, single-goal editing, 311-312 246-248 , 260-262 single responsibility principle (SRP), 99 , system architecture, 216-220 effect sketches, 168-171 simplifying signatures, preserving, 312-314 harnesses, 144-150 sketches effect sketches, simplifying, 168-171 for understanding code, 210-211 Reservation, 255 skinning and wrapping API calls, 205-207 Smalltalk, 45 snap(), 139 snarled methods, 292-294 software behavior, 5 changing, 3-8 risks of, 7-8 test coverings, 14-18 software vise, 10 Sprout Class (testing changes), 63-67 Sprout Method (testing changes) 59-63 , 246 SRP (single responsibility principle), 246-248 , 260-262 static cling, 369 static methods, 346 exposing, 345-347 Stra tegle S for dealing with responsibilities, 265 for monster methods extracting small pieces, 306 extractlng tO current class first, 306 finding sequences, 305-306 redoing extractions, 307 skeletonize methods 304-305 SubcIass and Override Method, 112 , 125 , 136 , 401-403 SubcIass to Override, 148 subclasses SubcIass and Override Method, 401-403 testing subclasses, 390 subclassing, programming by difference, 95-96 substituting functions, 377-378 subverting access protection, 141 Supercede lnstance Variable, 117-118 , 404-407

433 suspend frame method, 339 SymbolSource, 150 system architecture, preserving, 215-216 conversation concepts, 224 Naked CRC, 220-223 telling story of system, 216-220 tactics for dealing with responsibilities, 266-268 targeted testing, 190-194 TDD (Test-Driven DeveIopment), 20 , 88 -94 , 236-238 tearDown method 375 techniques, dependency-breaking techniques. See dependency-breaking techniques Template Redefinition, 408-411 templates, redefining, 408-411 temporal coupling, 67 test code versus production code, 110 test harnesses, 12 adding features, 87 parameters, 106-112 , 130-132 include dependencies, 127 ー 130 huge parameter lists, 116-118 hidden dependency, 113-116 global dependency, 118-126 aliased parameters, 133-136 getting classes intO Fitnesse, 53 FIT, 53 breaking dependencies, 79-85 Wrap Method, 67-70 Wrap Class, 71-76 Sprout Method, 59-63 Sprout Class, 63-67 and length of time for changes, 57-59 TESTING, 36 testEmpty method, 49 64 , 70 , 88-94 , 236-238 , 310 Test-Driven Development (TDD), 20 , 60 , test points, finding 19 side effects, 144-150 language features, 141-144 hidden methods, 138-141 getting methods intO INDEX testing, 9 around changes, 14 ー 18 higher-level testing, 14 instances, 121-123 for private methods, 138 proceduralcode, 231-232 function pointers, 23 8-239 migrating tO Object orientation, 239-244 Test-Driven Development (TDD), 236-238 With C macro preprocessor, 234-236 with 61e inclusion, 234-236 with Link Seam, 233-234 regression testing, 10-11 test harnesses, 12 unit testing, 12-14 unit-testing harnesses, 48 CppUnitLite, 50-52 JUnit, 49-50 NUnit, 52 testing subclasses, 227 , 390 TestingPager, 405 testS refactoring tools, 45-46 for effect reasoning, 165-167 tOOls time for changes time for changes, length of. See length of throwing exceptions, 89 Text Redefinition, 412-413 text, redefining, 412-413 for methods, 137 writing, 19 unit tests, pinch point traps, 184 method use rule, 189 lnterception P0ints, 174-182 higher-level tests, 173-174 fake objects, 26 directory locations for, 228-229 class naming conventions, 227-228 Characterization Tests, 151 , 157 targeted testing, 190-194 195 characterization tests, 186-190 , automated tests, 185-186 automated refactoring, 46-47

434 INDEX unlt-testing harnesses, 48 CppUnitLite, 50-52 JUnit, 49-50 NUnit, 52 TransactionLog, 366 TransactionManager, 350 TransactionRecorder, 365 type conversron errors, 193- UML notation, 221 understanding code, 209 194 deleting unused code, 213 effect on length of time for changes, 77-78 listing markup, 211-212 scratch refactoring, 212-213 sketches, 210-211 unit testing, 12-14 unit tests, pinch point traps, 184 unit-testing harnesses, CppUnitLite, 50-52 JUnit, 49-50 NUnit, 52 unused code, deleting, update method, 296 updateBalance, 370 48 213 validate method, 136 , 345 xUnit, 48 , 52 for methods, 137 tests, 19 null characters, 272 wrltlng Command class, 285 writeBody method, 281 LoginCommand, 272-273 Command class, 277 AddEmployeeCmd, 274 write method, 273-275 calls, 205-207 wrapping, skinning and wrapping API Wrap Method (testing changes), 67-70 Wrap CIass (testing changes), 71-76 WorkflowEngine, 350 systems, understanding Of code, 77 well-maintained systems versus legacy vise, 10 404-407 Supersede lnstance Variable, sensing variables, 301 Reservation class, 253 effects of change, 212 commandChar, 276-277 variables

奥付

The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty Of any kind and assume no responsibil- ity for errors or 01 れ ISSIOIIS. 、・ 0 liability iS assumed for incidental or consequen- tial damages ln connection with or arising out Of the use Of the informatlon or programs contained herein. Publisher: John Wait Editor in Chief: Don O'Hagan Acquisitions Editor: Paul Petralia Editorial Assistant: Michelle Vincenti Marketing Manager: Chris Guzikowski PubIicist: Kerry GuiIiano Cover Designer: Sandra Schroeder Managing Editor: Gina Kanouse Senior Project Editor: Lori Lyons Copy Editor: Krista Hansing lndexer: Lisa Stumpf Compositor: Karen Kennedy Proofreader: Debbie Williams Manufacturing Buyer: Dan Uhrig Prentice Hall offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, ・ WhiCh may include electronic verslons and/or custom covers and content particular tO your business, training goals, marketing focus, and branding in- terests. For more information, please contact: U. S. Corporate and Government Sales 1-800-382-3419 corpsales@pearsontechgroup.com For sales outside the U. S. , please contact: lnternational Sales 1-317-428-3341 international@pearsontechgroup.com Visit us on the web: www.phptr.com 信房 40 0 ー Co 〃 g ss C 酊 4 / og ⅲ g - - 乃ル〃〃 0 〃 D 酊 4 : 20047 08115 Copyright ◎ 2005 Pearson Education, lnc. Publishing as Prentice Hall PTR All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permisslon must be obtained from the publisher prior t0 any prohibited reproduction, storage in a retrieval system, or transm1SS10n 111 any forr れ or by any means, electronic, mechanical, photocopymg, recording, or likewise. For information regarding permlssrons, wrlte tO.• Pearson Education, lnc. Rights and Contracts Department One Lake Street Upper Saddle River, NJ 07458 Other product or company names mentioned herein are the trademarks or registered trademarks Of their respective owners. ISBN 0-13-117705-2 Text printed in the United States on recycled paper at RR Donnelley Seventh Printing, January 2008